
package com.wbtech.ums.controller;


import android.content.Context;
import android.os.Handler;
import com.wbtech.ums.common.AssembJSONObj;
import com.wbtech.ums.common.CommonUtil;
import com.wbtech.ums.common.NetworkUitlity;
import com.wbtech.ums.common.UmsConstants;
import com.wbtech.ums.dao.JSONParser;
import com.wbtech.ums.objects.MyMessage;
import com.wbtech.ums.objects.PostObjEvent;
import org.json.JSONObject;

public class EventController {
	
    static final String EVENTURL = UmsConstants.preUrl + UmsConstants.eventUrl;
    
    public static boolean postEventInfo(Handler handler, Context context, PostObjEvent event) {
        try {
            if (!event.verification())
            {
                CommonUtil.printLog("UMSAgent", "Illegal value of acc in postEventInfo");
                return false;
            }
            JSONObject localJSONObject = AssembJSONObj.getEventJOSNobj( event);

            if (1 == CommonUtil.getReportPolicyMode(context)
                    && CommonUtil.isNetworkAvailable(context)) {
                try {
                    MyMessage reslut = NetworkUitlity.post(EVENTURL, localJSONObject.toString());
                    if (!reslut.isFlag()) {   //local
                        CommonUtil.saveEventToDB(handler, "eventInfo", event, context);
                        return false;
                    }
                } catch (Exception e) {
                    CommonUtil.printLog("UmsAgent", "fail to post eventContent");
                }
            } else {
                CommonUtil.saveEventToDB(handler,"eventInfo", event, context);
                return false;
            }
        } catch (Exception e) {
            CommonUtil.printLog("UMSAgent", "Exception occurred in postEventInfo()");
            e.printStackTrace();
            return false;
        }
        return true;
    }
    

  
}
